﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.Models;
using KPIS.GERP.Models.FIM;
using KPIS.GERP.Models.SYS;
using WebMatrix.WebData;

namespace KPIS.GERP.WEB.GFMS.Controllers
{
    public class MasBankAccountController : Controller
    {
        public ActionResult Index()
        {
            var service = new MasBankAccountService.MasBankAccountServiceClient();
            return View(service.SelectBankAccount(new Models.FIM.MasBankAccount { RecordStatus = "!D" }, new[] { "Branch.Bank", "MasBudgetSource", "MasBankAccountType" }));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MasBankAccount model)
        {
            try
            {
                
                if (ModelState.IsValid)
                {
                    var service = new MasBankAccountService.MasBankAccountServiceClient();
                    var id = service.InsertBankAccount(model, WebSecurity.CurrentUserId);
                    return RedirectToAction("Index", new { id = id });

                }
            }
            catch (Exception ex)
            {

            }
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var service = new MasBankAccountService.MasBankAccountServiceClient();
            var model = service.SelectBankAccount(new Models.FIM.MasBankAccount { BankAccId = id }, null).FirstOrDefault();

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, MasBankAccount model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new MasBankAccountService.MasBankAccountServiceClient();
                    service.UpdateBankAccount(model, WebSecurity.CurrentUserId);

                    return RedirectToAction("Index", new { id = id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var service = new MasBankAccountService.MasBankAccountServiceClient();
                service.DeleteBankAccount(id, WebSecurity.CurrentUserId);

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAll(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new MasBankAccountService.MasBankAccountServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeleteBankAccount(delete, WebSecurity.CurrentUserId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }

        public ActionResult Details(int id)
        {
            var service = new MasBankAccountService.MasBankAccountServiceClient();
            var model = service.SelectBankAccount(new Models.FIM.MasBankAccount { BankAccId = id }, new[] { "MasBankAccountType" }).FirstOrDefault();

            return View(model);
        }

        public ActionResult BankBrabchList(int BankId)
        {
            var service = new MasBankService.MasBankServiceClient();
            var result = service.SelectListBankBranch(new Models.FIM.MasBankBranch { RecordStatus = "!D", BankId = BankId });
            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}
